﻿using System; 
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic; 
using System.Data;

namespace Sasc.PV.DAL
{
		/// <summary>
	/// ERPJiaoSe
	/// </summary>
		public partial class ERPJiaoSe:IPVDomain
	{ 
		#region Fields
	    	private int _id;		
		/// <summary>
		/// ID
        /// </summary>		
        public int ID
        {
            get{ return _id; }
            set{ _id = value; }
        }        
			private string _jiaosename;		
		/// <summary>
		/// JiaoSeName
        /// </summary>		
        public string JiaoSeName
        {
            get{ return _jiaosename; }
            set{ _jiaosename = value; }
        }        
			private string _backinfo;		
		/// <summary>
		/// BackInfo
        /// </summary>		
        public string BackInfo
        {
            get{ return _backinfo; }
            set{ _backinfo = value; }
        }        
			private string _quanxian;		
		/// <summary>
		/// QuanXian
        /// </summary>		
        public string QuanXian
        {
            get{ return _quanxian; }
            set{ _quanxian = value; }
        }        
		   		#endregion
   		
   		#region DAL Methods
		public bool Exists(int ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from ERPJiaoSe");
			strSql.Append(" where ");
			                                       strSql.Append(" ID = @ID  ");
                            			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
			parameters[0].Value = ID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}
		
		
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add()
		{
          return  Add(this);
		}
				
		
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(ERPJiaoSe model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into ERPJiaoSe(");			
            strSql.Append("JiaoSeName,BackInfo,QuanXian");
			strSql.Append(") values (");
            strSql.Append("@JiaoSeName,@BackInfo,@QuanXian");            
            strSql.Append(") ");            
            strSql.Append(";select @@IDENTITY");		
			SqlParameter[] parameters = {
			            new SqlParameter("@JiaoSeName", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@BackInfo", SqlDbType.NVarChar,1000) ,            
                        new SqlParameter("@QuanXian", SqlDbType.NText)             
              
            };
			            
            parameters[0].Value = model.JiaoSeName;                        
            parameters[1].Value = model.BackInfo;                        
            parameters[2].Value = model.QuanXian;                        
			   
			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);			
			if (obj == null)
			{
				return 0;
			}
			else
			{
				                    
            	return Convert.ToInt32(obj);
                                                                  
			}			   
            			
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update()
		{
			return Update(this);
		}
		
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(ERPJiaoSe model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update ERPJiaoSe set ");
			                                                
            strSql.Append(" JiaoSeName = @JiaoSeName , ");                                    
            strSql.Append(" BackInfo = @BackInfo , ");                                    
            strSql.Append(" QuanXian = @QuanXian  ");            			
			strSql.Append(" where ID=@ID ");
						
SqlParameter[] parameters = {
			            new SqlParameter("@ID", SqlDbType.Int,4) ,            
                        new SqlParameter("@JiaoSeName", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@BackInfo", SqlDbType.NVarChar,1000) ,            
                        new SqlParameter("@QuanXian", SqlDbType.NText)             
              
            };
						            
            parameters[0].Value = model.ID;                        
            parameters[1].Value = model.JiaoSeName;                        
            parameters[2].Value = model.BackInfo;                        
            parameters[3].Value = model.QuanXian;                        
            int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from ERPJiaoSe ");
			strSql.Append(" where ID=@ID");
						SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
			parameters[0].Value = ID;


			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
				/// <summary>
		/// 批量删除一批数据
		/// </summary>
		public bool DeleteList(string IDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from ERPJiaoSe ");
			strSql.Append(" where ID in ("+IDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
				
		
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public ERPJiaoSe GetModel(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ID, JiaoSeName, BackInfo, QuanXian  ");			
			strSql.Append("  from ERPJiaoSe ");
			strSql.Append(" where ID=@ID");
						SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
			parameters[0].Value = ID;

			
			ERPJiaoSe model=new ERPJiaoSe();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			
			if(ds.Tables[0].Rows.Count>0)
			{
												if(ds.Tables[0].Rows[0]["ID"].ToString()!="")
				{
					model.ID=int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
				}
																																				model.JiaoSeName= ds.Tables[0].Rows[0]["JiaoSeName"].ToString();
																																model.BackInfo= ds.Tables[0].Rows[0]["BackInfo"].ToString();
																																model.QuanXian= ds.Tables[0].Rows[0]["QuanXian"].ToString();
																										
				return model;
			}
			else
			{
				return null;
			}
		}
		
		
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM ERPJiaoSe ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}
		
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" * ");
			strSql.Append(" FROM ERPJiaoSe ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}
		
		
		        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="pageIndex">从0开始</param>
        /// <param name="pageSize"></param>
        /// <param name="strWhere"></param>
        /// <param name="filedOrder"></param>
        /// <returns></returns>
		public DataSet GetList(int pageIndex,int pageSize, string strWhere  = null,string filedOrder = null)
		{
			var startIndex = pageIndex * pageSize + 1;
			var endIndex = (pageIndex + 1) * pageSize;
			var totalCount = 0;
			
			var totalCountSqlBuilder = new StringBuilder();
			totalCountSqlBuilder.Append(" SELECT COUNT([ID]) FROM ERPJiaoSe ");
			if(!string.IsNullOrEmpty(strWhere))
			{
				totalCountSqlBuilder.Append(" where "+strWhere);
			}
			totalCount = (int)DbHelperSQL.GetSingle(totalCountSqlBuilder.ToString());
			
				StringBuilder strSql=new StringBuilder();
				strSql.Append("select TOP "+pageSize.ToString()+" t.* from ");
				strSql.Append("(select ");
				strSql.Append(" row_number() over (" );
				if(!string.IsNullOrEmpty(filedOrder))
				{
					strSql.Append(" order by " + filedOrder);
				}
				else 
				{
					strSql.Append(" ORDER BY [ID] ");
				}
				strSql.Append(") AS ___rownum, ");
				
				strSql.Append(" * ");
				strSql.Append(" FROM ERPJiaoSe ");
				if(!string.IsNullOrEmpty(strWhere))
				{
					strSql.Append(" where "+strWhere);
				}
				strSql.Append(") t ");
				strSql.Append(" where t.___rownum>="+startIndex.ToString()+" AND t.___rownum<="+endIndex.ToString());
				if(!string.IsNullOrEmpty(filedOrder))
				{
					strSql.Append(" order by " + filedOrder);
				}
				
				var dataSet = DbHelperSQL.Query(strSql.ToString());
					
            if (totalCount > 0 && startIndex <= totalCount)
			{		
				for(var i=1;i<startIndex;i++)
				{
					dataSet.Tables[0].Rows.InsertAt(dataSet.Tables[0].NewRow(),0);				
				}
				for(var i=endIndex;i<totalCount;i++)
				{
					dataSet.Tables[0].Rows.Add(dataSet.Tables[0].NewRow());
				}
			}
			return dataSet;
		}
				#endregion
   
   		#region IPVDomain
   		public string GetDbTableName()
   		{
   			return "ERPJiaoSe";
   		}
   		#endregion
	}
}

